import DBConf from '#Config/database.js'
import { dataGet } from '#Utils/helper.js'

/** @type {import('sequelize-cli').Migration} */
export default {
    async up(queryInterface, Sequelize) {
        /**
         * Add altering commands here.
         *
         * Example:
         * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
         */
        const currentDatabase = queryInterface.sequelize.config.database;

        const publicDB = dataGet(DBConf, 'defaultDB');

        if (currentDatabase !== publicDB) {
            return
        }
        await queryInterface.createTable('contents', {
            id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: Sequelize.INTEGER
            },
            createdAt: {
                allowNull: false,
                type: Sequelize.DATE,
                allowNull: false,
                defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
                field: 'created_at',
                comment: '创建时间'
            },
            updatedAt: {
                allowNull: false,
                type: Sequelize.DATE,
                allowNull: false,
                defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
                field: 'updated_at',
                comment: '更新时间'
            },
            deletedAt: {
                type: Sequelize.DATE,
                allowNull: true,
                field: 'deleted_at',
                comment: '删除时间'
            },
            publishedAt: {
                type: Sequelize.DATE,
                allowNull: true,
                field: 'published_at',
                comment: '发布时间'
            },
            title: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: '',
                comment: '标题'
            },
            slug: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: '',
                comment: '别名'
            },
            text: {
                type: Sequelize.TEXT,
                allowNull: false,
                defaultValue: '',
                comment: '内容'
            },
            status: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: 'draft',
                comment: '状态'
            },
            password: {
                type: Sequelize.STRING,
                allowNull: true,
                defaultValue: '',
                comment: '密码'
            },
            allowComment: {
                type: Sequelize.BOOLEAN,
                allowNull: false,
                defaultValue: true,
                field: 'allow_comment',
                comment: '是否允许评论'
            },
            pid: {
                type: Sequelize.INTEGER,
                allowNull: false,
                defaultValue: 0,
                comment: '父级ID'
            },
            stars: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: '0',
                comment: '点赞数'
            },
            views: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: '0',
                comment: '浏览数'
            }

        })
    },

    async down(queryInterface, Sequelize) {
        /**
         * Add reverting commands here.
         *
         * Example:
         * await queryInterface.dropTable('users');
         */
        const currentDatabase = queryInterface.sequelize.config.database;

        const publicDB = dataGet(DBConf, 'defaultDB');

        if (currentDatabase !== publicDB) {
            return
        }

        await queryInterface.dropTable('contents')
    }
};
